window.apiUrl = "http://120.27.155.103:8080/gmms_web_springmvc/ship";

var appXhr = {
    AJAX: function(isDefaultApiUrl,url, method, params, statusCode) {
        var that = this;
        url = isDefaultApiUrl ? url :  apiUrl + url;
        var def = $.ajax({
                url: url,
                timeout : 10000,
                type: method ? method : 'GET',
                statusCode: statusCode,
                data: params
            })
        .done(function(data){
            if(!isDefaultApiUrl &&　data.code !== 100){
                F7.alert("code:"+data.code+"<br><br>"+data.message);
            }
        })
        .fail(function(xhr,statusCode,statusText) {
            F7.alert("code:"+xhr.status+"<br><br>"+statusText);
        })
        .always(function(xhr,status){
            if(status === 'timeout'){
                F7.hideIndicator();
                F7.alert('连接超时！您可以重试看看');
            }
            F7.hideIndicator();
        });
        

        return def;

    },
    POST: function() {
        var isDefaultApiUrl,url,params,statusCode;
        if(typeof arguments[0] === 'boolean') {
            isDefaultApiUrl = arguments[0];
            url = arguments[1];
            params = arguments[2];
            statusCode = arguments[3];
        }else{
            isDefaultApiUrl = false;
            url = arguments[0];
            params = arguments[1];
            statusCode = arguments[2];
        }
        var postDef = this.AJAX(isDefaultApiUrl,url, 'POST', params, statusCode);
        return postDef;
    },
    GET: function() {
        var isDefaultApiUrl,url,params,statusCode;
        if(typeof arguments[0] === 'boolean') {
            isDefaultApiUrl = arguments[0];
            url = arguments[1];
            params = arguments[2];
            statusCode = arguments[3];
        }else{
            isDefaultApiUrl = false;
            url = arguments[0];
            params = arguments[1];
            statusCode = arguments[2];
        }

        var getDef = this.AJAX(isDefaultApiUrl,url, 'GET', params, statusCode);
        return getDef;
    }
};

module.exports = appXhr;
